<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-PBHRK7');</script>
<!-- End Google Tag Manager -->
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="经验积累," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.2" />






<meta name="description" content="前言现在已处于微服务架构盛世，面对我们各种多语言的微服务，如果没有一套服务注册服务发现的产品服务，我们还是会觉得很慌， 不过今天 少年你有幸看到我这边文章， 你可以不用慌了！ Consul 简介Consul 是什么?Consul 是 HashiCorp 开源的，并且有服务发现，key/value存储, 多数据中心,健康心跳检查的开源产品. Consul 架构 上图很好的展示了Consul对于多数据">
<meta name="keywords" content="经验积累">
<meta property="og:type" content="article">
<meta property="og:title" content="Consul 进行Egg.js 与 Django 服务发现部署">
<meta property="og:url" content="http://wurh.github.io/2019/11/14/201911141933/index.html">
<meta property="og:site_name" content="华Ing&#39;s blog">
<meta property="og:description" content="前言现在已处于微服务架构盛世，面对我们各种多语言的微服务，如果没有一套服务注册服务发现的产品服务，我们还是会觉得很慌， 不过今天 少年你有幸看到我这边文章， 你可以不用慌了！ Consul 简介Consul 是什么?Consul 是 HashiCorp 开源的，并且有服务发现，key/value存储, 多数据中心,健康心跳检查的开源产品. Consul 架构 上图很好的展示了Consul对于多数据">
<meta property="og:locale" content="en">
<meta property="og:image" content="https://www.consul.io/assets/images/consul-arch-420ce04a.png">
<meta property="og:image" content="https://wurh.github.io/images/blogs/20191114/pc-01.png">
<meta property="og:image" content="https://wurh.github.io/images/blogs/20191114/pc-02.png">
<meta property="og:image" content="https://wurh.github.io/images/blogs/20191114/pc-03.png">
<meta property="og:updated_time" content="2019-11-14T11:35:27.958Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Consul 进行Egg.js 与 Django 服务发现部署">
<meta name="twitter:description" content="前言现在已处于微服务架构盛世，面对我们各种多语言的微服务，如果没有一套服务注册服务发现的产品服务，我们还是会觉得很慌， 不过今天 少年你有幸看到我这边文章， 你可以不用慌了！ Consul 简介Consul 是什么?Consul 是 HashiCorp 开源的，并且有服务发现，key/value存储, 多数据中心,健康心跳检查的开源产品. Consul 架构 上图很好的展示了Consul对于多数据">
<meta name="twitter:image" content="https://www.consul.io/assets/images/consul-arch-420ce04a.png">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: 'Author'
    }
  };
</script>




  <link rel="canonical" href="http://wurh.github.io/2019/11/14/201911141933/"/>


  <title> Consul 进行Egg.js 与 Django 服务发现部署 | 华Ing's blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-PBHRK7"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

  










  
  
    
  

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-69412379-1', 'auto');
  ga('send', 'pageview');

</script>

  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b3ddfb8655d8b77429e04c7a4d5bcbc1";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
  </script>


  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">华Ing's blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">harder and harder</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Consul 进行Egg.js 与 Django 服务发现部署
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2019-11-14T19:36:00+08:00" content="2019-11-14">
              2019-11-14
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/服务端/" itemprop="url" rel="index">
                    <span itemprop="name">服务端</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2019/11/14/201911141933/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2019/11/14/201911141933/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>现在已处于微服务架构盛世，面对我们各种多语言的微服务，如果没有一套服务注册服务发现的产品服务，我们还是会觉得很慌， 不过今天 少年你有幸看到我这边文章， 你可以不用慌了！</p>
<h2 id="Consul-简介"><a href="#Consul-简介" class="headerlink" title="Consul 简介"></a>Consul 简介</h2><h3 id="Consul-是什么"><a href="#Consul-是什么" class="headerlink" title="Consul 是什么?"></a>Consul 是什么?</h3><p>Consul 是 HashiCorp 开源的，并且有<code>服务发现</code>，<code>key/value存储</code>, <code>多数据中心</code>,<code>健康心跳检查</code>的开源产品.</p>
<h3 id="Consul-架构"><a href="#Consul-架构" class="headerlink" title="Consul 架构"></a>Consul 架构</h3><p><img src="https://www.consul.io/assets/images/consul-arch-420ce04a.png" alt=""></p>
<p>上图很好的展示了Consul对于多数据中心的支持另外在两个数据中心之间只有Service层可以相互通信。</p>
<h2 id="Consul-安装"><a href="#Consul-安装" class="headerlink" title="Consul 安装"></a>Consul 安装</h2><h3 id="Linux-Centos-安装"><a href="#Linux-Centos-安装" class="headerlink" title="Linux Centos 安装"></a>Linux Centos 安装</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">wget https://releases.hashicorp.com/consul/1.4.0/consul_1.4.0_linux_amd64.zip</span><br></pre></td></tr></table></figure>
<p>在当前目录下解压， 然后配置环境变量</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">vim -e ~/.bashrc</span><br><span class="line"></span><br><span class="line">exports PATH=/opt/soft:$PATH</span><br></pre></td></tr></table></figure>
<p>验证一下consul是否安装成功</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">consul </span><br><span class="line">root@iZbp1isjfk2rw8fpnxx8wgZ ~]# consul</span><br><span class="line">Usage: consul [--version] [--help] &lt;command&gt; [&lt;args&gt;]</span><br><span class="line"></span><br><span class="line">Available commands are:</span><br><span class="line">    acl            Interact with Consul's ACLs</span><br><span class="line">    agent          Runs a Consul agent</span><br><span class="line">    catalog        Interact with the catalog</span><br><span class="line">    connect        Interact with Consul Connect</span><br><span class="line">    debug          Records a debugging archive for operators</span><br><span class="line">    event          Fire a new event</span><br><span class="line">    exec           Executes a command on Consul nodes</span><br><span class="line">    force-leave    Forces a member of the cluster to enter the "left" state</span><br><span class="line">    info           Provides debugging information for operators.</span><br><span class="line">    intention      Interact with Connect service intentions</span><br><span class="line">    join           Tell Consul agent to join cluster</span><br><span class="line">    keygen         Generates a new encryption key</span><br><span class="line">    keyring        Manages gossip layer encryption keys</span><br><span class="line">    kv             Interact with the key-value store</span><br><span class="line">    leave          Gracefully leaves the Consul cluster and shuts down</span><br><span class="line">    lock           Execute a command holding a lock</span><br><span class="line">    maint          Controls node or service maintenance mode</span><br><span class="line">    members        Lists the members of a Consul cluster</span><br><span class="line">    monitor        Stream logs from a Consul agent</span><br><span class="line">    operator       Provides cluster-level tools for Consul operators</span><br><span class="line">    reload         Triggers the agent to reload configuration files</span><br><span class="line">    rtt            Estimates network round trip time between nodes</span><br><span class="line">    services       Interact with services</span><br><span class="line">    snapshot       Saves, restores and inspects snapshots of Consul server state</span><br><span class="line">    validate       Validate config files/directories</span><br><span class="line">    version        Prints the Consul version</span><br><span class="line">    watch          Watch for changes in Consul</span><br></pre></td></tr></table></figure>
<p>出现以上信息则安装成功</p>
<h3 id="Consul-Agent"><a href="#Consul-Agent" class="headerlink" title="Consul Agent"></a>Consul Agent</h3><p>执行<code>consul agent -dev</code>启动开发模式这个模式会快速启动一个单节点的Consul。注意这个模式不能数据持久化因此不能用于生产环境</p>
<blockquote>
<p>启动命令简介</p>
</blockquote>
<pre><code>- server定义agent运行在server模式每个数据中心的Server建议在35个避免失败情况下数据的丢失
- client定义agent运行在client模式
- bootstrap-expect在一个datacenter中期望提供的server节点数目当该值提供的时候consul一直等到达到指定sever数目的时候才会引导整个集群
- bind节点的ip地址一般是0.0.0.0或云服务内网地址用于被集群中的其他节点所访问
- node指定节点在集群中的唯一名称默认为机器的hostname
- config-dir配置文件目录里面所有以.json结尾的文件都会被加载
</code></pre><h3 id="启动Consul-Server"><a href="#启动Consul-Server" class="headerlink" title="启动Consul Server"></a>启动Consul Server</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">consul agent -data-dir /tmp/node0 -node=node0 -bind=127.0.0.1:8500 -datacenter=dc1 -ui -client=127.0.0.1 -server -bootstrap-expect 1</span><br></pre></td></tr></table></figure>
<p>再启动</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">consul agent -dev -ui -node=consul-dev -client=127.0.0.1:8500</span><br></pre></td></tr></table></figure>
<p>停止</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">consul leave -rpc-addr=127.0.0.1:8500</span><br></pre></td></tr></table></figure>
<p>接着访问 <a href="http://127.0.0.1:8500/ui/dc1/services" target="_blank" rel="noopener">http://127.0.0.1:8500/ui/dc1/services</a> 我们就可以看到界面了</p>
<p><img src="https://wurh.github.io/images/blogs/20191114/pc-01.png" alt=""></p>
<h2 id="Egg-js-接入"><a href="#Egg-js-接入" class="headerlink" title="Egg.js 接入"></a>Egg.js 接入</h2><h3 id="使用egg-consul-plus进行上报"><a href="#使用egg-consul-plus进行上报" class="headerlink" title="使用egg-consul-plus进行上报"></a>使用egg-consul-plus进行上报</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install egg-consul-plus --save-dev</span><br></pre></td></tr></table></figure>
<p>plugin.js</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="string">'consulPlus'</span>: &#123;</span><br><span class="line">  enable: <span class="literal">true</span>,</span><br><span class="line">  package: <span class="string">'egg-consul-plus'</span>,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>config.default.js</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">  config.consul = &#123;</span><br><span class="line">    server: &#123;</span><br><span class="line">        host: <span class="string">'127.0.0.1'</span>, <span class="comment">// 注册中心ip地址</span></span><br><span class="line">        port: <span class="number">8500</span> <span class="comment">// 注册中心端口号</span></span><br><span class="line">    &#125;,</span><br><span class="line">    services: [ <span class="comment">// 服务发现列表</span></span><br><span class="line">        &#123;</span><br><span class="line">            referName: <span class="string">'consulPlusTest'</span>, <span class="comment">// 引用名，后续可用 app.services.referName 访问服务</span></span><br><span class="line">            comment: <span class="string">'consulPlusTest'</span>, <span class="comment">// 备注</span></span><br><span class="line">            serviceId: <span class="string">'consul-plus-test'</span> <span class="comment">// 服务id</span></span><br><span class="line">        &#125;</span><br><span class="line">    ],</span><br><span class="line">    register: <span class="literal">true</span>, <span class="comment">// 是否注册当前模块，默认为false</span></span><br><span class="line">    multiInstance: <span class="literal">true</span>, <span class="comment">// 多实例模式开关，默认为false，注意当开启多实例，务必保证集群中的每个项目的keys不同，或者会导致先启动的项目被隔离(被覆盖)</span></span><br><span class="line">    name: <span class="string">'consul-plus-test'</span>, <span class="comment">// 注册id</span></span><br><span class="line">    tags: [<span class="string">'consul-plus-test'</span>], <span class="comment">// 标签信息</span></span><br><span class="line">    check: &#123;</span><br><span class="line">        http: <span class="string">'http://10.18.124.230:7001'</span>, <span class="comment">// 健康检测地址</span></span><br><span class="line">        interval: <span class="string">'5s'</span>, <span class="comment">// 健康检测间隔</span></span><br><span class="line">        notes: <span class="string">'http service check'</span>,</span><br><span class="line">        status: <span class="string">'critical'</span></span><br><span class="line">    &#125;,</span><br><span class="line">    address: <span class="string">'127.0.0.1'</span>, <span class="comment">// 当前模块的注册地址</span></span><br><span class="line">    port: <span class="number">7001</span> <span class="comment">// 当前模块的注册端口号</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>最后我们启动完服务就可以看到结果了</p>
<p><img src="https://wurh.github.io/images/blogs/20191114/pc-02.png" alt=""></p>
<h2 id="Django接入"><a href="#Django接入" class="headerlink" title="Django接入"></a>Django接入</h2><h3 id="python-consul-插件安装"><a href="#python-consul-插件安装" class="headerlink" title="python-consul 插件安装"></a>python-consul 插件安装</h3><p>requirements.txt 增加 python-consul==1.1.0</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip3 install -r requirements.txt</span><br></pre></td></tr></table></figure>
<p>接着创建 backend.consul_service.py</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#pip install python-consul</span></span><br><span class="line">import consul</span><br><span class="line"></span><br><span class="line">class Consul(object):</span><br><span class="line">    def __init__(self, host, port):</span><br><span class="line">        <span class="string">''</span><span class="string">'初始化，连接consul服务器'</span><span class="string">''</span></span><br><span class="line">        self._consul = consul.Consul(host, port)</span><br><span class="line"></span><br><span class="line">    def RegisterService(self, name, host, port, tags=None):</span><br><span class="line">        tags = tags or []</span><br><span class="line">        <span class="comment"># 注册服务</span></span><br><span class="line">        self._consul.agent.service.register(</span><br><span class="line">            name,</span><br><span class="line">            name,</span><br><span class="line">            host,</span><br><span class="line">            port,</span><br><span class="line">            tags,</span><br><span class="line">            <span class="comment"># 健康检查ip端口，检查时间：5,超时时间：30，注销时间：30s</span></span><br><span class="line">            check=consul.Check().tcp(host, port, <span class="string">"5s"</span>, <span class="string">"30s"</span>, <span class="string">"30s"</span>))</span><br><span class="line"></span><br><span class="line">    def GetService(self, name):</span><br><span class="line">        services = self._consul.agent.services()</span><br><span class="line">        service = services.get(name)</span><br><span class="line">        <span class="keyword">if</span> not service:</span><br><span class="line">            <span class="built_in">return</span> None, None</span><br><span class="line">        addr = <span class="string">"&#123;0&#125;:&#123;1&#125;"</span>.format(service[<span class="string">'Address'</span>], service[<span class="string">'Port'</span>])</span><br><span class="line">        <span class="built_in">return</span> service, addr</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    host=<span class="string">"10.18.120.228"</span> <span class="comment">#consul服务器的ip</span></span><br><span class="line">    port=8500 <span class="comment">#consul服务器对外的端口</span></span><br><span class="line">    consul_client=Consul(host,port)</span><br><span class="line"></span><br><span class="line">    name=<span class="string">"weridegoPython"</span></span><br><span class="line">    host=<span class="string">"10.18.124.230"</span></span><br><span class="line">    port=7106</span><br><span class="line">    consul_client.RegisterService(name,host,port)</span><br><span class="line"></span><br><span class="line">    check = consul.Check().tcp(host, port, <span class="string">"5s"</span>, <span class="string">"30s"</span>, <span class="string">"30s"</span>)</span><br><span class="line">    <span class="built_in">print</span>(check)</span><br><span class="line">    res=consul_client.GetService(<span class="string">"maple"</span>)</span><br><span class="line">    <span class="built_in">print</span>(res)</span><br></pre></td></tr></table></figure>
<p>在启动时候注册改对象 修改 wsgi.py</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">WSGI config for backend project.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">It exposes the WSGI callable as a module-level variable named ``application``.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">For more information on this file, see</span></span><br><span class="line"><span class="string">https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> django.core.wsgi <span class="keyword">import</span> get_wsgi_application</span><br><span class="line"><span class="keyword">from</span> backend.consul_service <span class="keyword">import</span> Consul</span><br><span class="line"></span><br><span class="line">con = Consul(<span class="string">'127.0.0.0'</span>,<span class="number">8500</span>)</span><br><span class="line">con.RegisterService(</span><br><span class="line">    <span class="string">"weridegoPython"</span>,</span><br><span class="line">    <span class="string">"127.0.0.0"</span>,</span><br><span class="line">    <span class="number">7106</span>,</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">os.environ.setdefault(<span class="string">"DJANGO_SETTINGS_MODULE"</span>, <span class="string">"backend.settings"</span>)</span><br><span class="line"></span><br><span class="line">application = get_wsgi_application()</span><br></pre></td></tr></table></figure>
<p>接着我们就可以看到效果了</p>
<p><img src="https://wurh.github.io/images/blogs/20191114/pc-03.png" alt=""></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="https://wurh.github.io/images/others/weixinshou.png" alt="华Ing WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="https://wurh.github.io/images/others/alifu.png" alt="华Ing Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/经验积累/" rel="tag">#经验积累</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/11/12/201911121536/" rel="next" title="Flutter实践 -- 按钮组件">
                <i class="fa fa-chevron-left"></i> Flutter实践 -- 按钮组件
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        
<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='//bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2019/11/14/201911141933/"
           data-title="Consul 进行Egg.js 与 Django 服务发现部署" data-url="http://wurh.github.io/2019/11/14/201911141933/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="https://wurh.github.io/images/avatar/logo.jpg?123456"
               alt="华Ing" />
          <p class="site-author-name" itemprop="name">华Ing</p>
          <p class="site-description motion-element" itemprop="description">harder and harder</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">56</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/wurh" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/1674582564/profile?topnav=1&wvr=6" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.zhihu.com/people/wu-rong-hua-56" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://fsiaonma.github.io/" title="sam博客" target="_blank">sam博客</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://shenjoel.github.io/" title="joel博客" target="_blank">joel博客</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://firewood1122.github.io" title="firewood博客" target="_blank">firewood博客</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://reqianduan.com" title="热前端" target="_blank">热前端</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://opentutorial.info/" title="黄超博客" target="_blank">黄超博客</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Consul-简介"><span class="nav-number">2.</span> <span class="nav-text">Consul 简介</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Consul-是什么"><span class="nav-number">2.1.</span> <span class="nav-text">Consul 是什么?</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Consul-架构"><span class="nav-number">2.2.</span> <span class="nav-text">Consul 架构</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Consul-安装"><span class="nav-number">3.</span> <span class="nav-text">Consul 安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Linux-Centos-安装"><span class="nav-number">3.1.</span> <span class="nav-text">Linux Centos 安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Consul-Agent"><span class="nav-number">3.2.</span> <span class="nav-text">Consul Agent</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#启动Consul-Server"><span class="nav-number">3.3.</span> <span class="nav-text">启动Consul Server</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Egg-js-接入"><span class="nav-number">4.</span> <span class="nav-text">Egg.js 接入</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#使用egg-consul-plus进行上报"><span class="nav-number">4.1.</span> <span class="nav-text">使用egg-consul-plus进行上报</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Django接入"><span class="nav-number">5.</span> <span class="nav-text">Django接入</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#python-consul-插件安装"><span class="nav-number">5.1.</span> <span class="nav-text">python-consul 插件安装</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2013 - 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">华Ing</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.2"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"wurh"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  

  


</body>
</html>
